#include "editablemodel.h"

EditableModel::EditableModel( QWidget* parent )
	 : QTableView(parent)
{
	
	
}

void  EditableModel::setDB( QSqlDatabase dbin )
{
	db = dbin;
  orderpaint = Qt::AscendingOrder;
	model = new QSqlTableModel(this,db);
  model->setEditStrategy(QSqlTableModel::OnManualSubmit);
}

void EditableModel::PaintTable( const QString table  )
{
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    model = new QSqlTableModel(this,db);
	  model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->clear();
    model->setTable(table);
    model->select();
    setModel(model);
    setAlternatingRowColors (true) ;
    resizeRowsToContents();
    resizeColumnsToContents();
    shortcut0 = new QShortcut(QKeySequence("F5"),this);
    connect(shortcut0, SIGNAL(activated()),this, SLOT(Reload()));
    shortcut1 = new QShortcut(QKeySequence::Save,this);
    connect(shortcut1, SIGNAL(activated()),model, SLOT(submitAll()));
    

    horizontalheader = horizontalHeader();
    connect (horizontalheader,SIGNAL(sectionClicked(int)),this,SLOT(sortColumn(int)));
    QApplication::restoreOverrideCursor();
    
}

void EditableModel::sortColumn( int cool ) 
{
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));

    
    if (orderpaint == Qt::AscendingOrder) {
        orderpaint = Qt::DescendingOrder; /* next invert */
    } else {
        orderpaint = Qt::AscendingOrder;  /* next invert */
    }
    model->setSort(cool,orderpaint);
    Reload();
    QApplication::restoreOverrideCursor();
}


void  EditableModel::Reload()
{
	    model->select();
      setModel(model);
      resizeRowsToContents();
      resizeColumnsToContents();
}



void  EditableModel::contextMenuEvent ( QContextMenuEvent * e )  
{
    if (model && !model->lastError().isValid())  {
    QMenu *UserMenu =CreateMenu();
    UserMenu->exec(QCursor::pos());
    delete UserMenu;
    return;
    }
}

void  EditableModel::SaveData() 
{
     model->database().transaction();
    if (model->submitAll()) {
    model->database().commit();
    } else {
    model->database().rollback();
    QMessageBox::warning(0, tr("Cached Table"),tr("The database reported an error: %1").arg(model->lastError().text()));
    model->revertAll();
    }
}

QMenu *EditableModel::CreateMenu()
{
    ActionCommit = new QAction(this);
    ActionCommit->setText(tr("Save current items"));
    ActionCommit->setIcon(QIcon(QString::fromUtf8(":/img/filesave.png")));
    ActionCommit->setStatusTip(tr("Save current items")); 
    ActionCommit->setShortcut(QKeySequence::Save);
    connect(ActionCommit, SIGNAL(triggered()),this,  SLOT(SaveData()));
    
    
    ActionReload = new QAction(this);
    ActionReload->setText(tr("Reload Table"));
    ActionReload->setIcon(QIcon(QString::fromUtf8(":/img/view_text.png")));
    ActionReload->setStatusTip(tr("Reload Table")); 
    ActionReload->setShortcut(tr("F5"));
    connect(ActionReload, SIGNAL(triggered()), this,  SLOT(Reload()));
    
    
    ActionUndoUpdate = new QAction(this);
    ActionUndoUpdate->setText(tr("Revert pending"));
    ActionUndoUpdate->setIcon(QIcon(QString::fromUtf8(":/img/reload_page.png")));
    ActionUndoUpdate->setStatusTip(tr("Revert pending")); 
    connect(ActionUndoUpdate, SIGNAL(triggered()), model,  SLOT(revertAll()));
    
    
    ActionappendLine = new QAction(this);
    ActionappendLine->setText(tr("Insert new line"));
    ActionappendLine->setIcon(QIcon(QString::fromUtf8(":/img/7days.png")));
    ActionappendLine->setStatusTip(tr("Insert new line")); 
    connect(ActionappendLine, SIGNAL(triggered()), this,  SLOT(insert_line()));

    QMenu *TC = new QMenu(); 
    TC->addAction(ActionCommit);
    TC->addAction(ActionReload);
    TC->addAction(ActionUndoUpdate);
    TC->addAction(ActionappendLine);
    return TC;
    
    
}

void EditableModel::insert_line()
{
model->insertRow(0);
}

















